import 'package:any_reader/config/resource_manager.dart';
import 'package:any_reader/public.dart';

class BookItem extends StatelessWidget {
  final Book item;
  final BookSheetModel model;
  BookItem({@required this.item, this.model});
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      decoration: new BoxDecoration(
          border: Border(
              right: BorderSide(color: Color(0xFFebedf0), width: 0.8),
              bottom: BorderSide(color: Color(0xFFebedf0), width: 0.8))),
      child: Padding(
        padding: EdgeInsets.all(ScreenUtil().setWidth(0)),
        child: new InkWell(
            onLongPress: () {
              showDialog(
                context: context,
                builder: (ctx) => Padding(
                  padding: EdgeInsets.all(ScreenUtil().setWidth(8)),
                  child: CustomDialog(
                    title: "确认删除？",
                    confirm: () {
                      model.deleteBook(item.bookId);
                    },
                  ),
                ),
              );
            },
            onTap: () {
              toBookContent(context, item);
            },
            child: Column(
              children: <Widget>[
                Container(
                  padding: EdgeInsets.fromLTRB(0, ScreenUtil().setWidth(20), 0,
                      ScreenUtil().setWidth(20)),
                  margin:
                      EdgeInsets.fromLTRB(0, ScreenUtil().setHeight(10), 0, 0),
                  child: SizedBox(
                    child: new Stack(
                      alignment: AlignmentDirectional.center,
                      children: <Widget>[
                        Container(
                          width: ScreenUtil().setWidth((750 - 120) / 4),
                          height: ScreenUtil().setHeight((1334) / 7),
                          child: item.imgUrl == "" || item.imgUrl == null
                              ? Image.asset(
                                  ImageHelper.wrapAssets(
                                      'cover/city/cover_3.png'),
                                  fit: BoxFit.fill)
                              : CachedImage(
                                  url: item.imgUrl, fit: BoxFit.fitHeight),
                          decoration: BoxDecoration(
                            boxShadow: [
                              BoxShadow(
                                  color: Color.fromARGB(100, 255, 255, 255),
                                  blurRadius: 3,
                                  spreadRadius: 0)
                            ],
                          ),
                        ),
                        Positioned(
                          left: 10,
                          right: 10,
                          top: 15,
                          child: Container(
                            padding: EdgeInsets.fromLTRB(
                                ScreenUtil().setWidth(15),
                                0,
                                ScreenUtil().setWidth(15),
                                0),
                            child: new Text(
                              item.imgUrl == "" || item.imgUrl == null
                                  ? item.title
                                  : '',
                              style: new TextStyle(fontSize: 10),
                              textAlign: TextAlign.center,
                            ),
                          ),
                        ),
                        Positioned(
                          right: 0,
                          top: ScreenUtil().setHeight(2),
                          child: Container(
                            decoration: BoxDecoration(
                                color: Colors.red,
                                borderRadius: BorderRadius.circular(2)),
                            padding: EdgeInsets.fromLTRB(
                                ScreenUtil().setWidth(15),
                                0,
                                ScreenUtil().setWidth(15),
                                0),
                            child: new Text(
                              "源" + item.reptileType,
                              style: new TextStyle(
                                  fontSize: 10, color: Colors.white),
                              textAlign: TextAlign.center,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                Container(
                  child: Text(
                    ((int.parse(item.now) / int.parse(item.total)) * 100)
                            .toStringAsFixed(2) +
                        "%",
                    style: TextStyle(
                      fontSize: ScreenUtil().setSp(18),
                      color: Color(0xFF666666),
                    ),
                  ),
                )
              ],
            )),
      ),
    );
  }

  toBookContent(BuildContext context, Book item) async {
    Chapter _chapter =
        await DatabaseHelper().findChapter(item.bookId, item.now);
    String bookId = Uri.encodeComponent(_chapter.bookId);
    String catalogId = Uri.encodeComponent(_chapter.catalogId);
    String num = Uri.encodeComponent(_chapter.num);
    String position = Uri.encodeComponent("0");
    var route = "/reader?"
        "bookId=$bookId&"
        "catalogId=$catalogId&"
        "num=$num&"
        "position=$position";
    AppConfig.router
        .navigateTo(context, route, transition: TransitionType.inFromRight);
    FlutterStatusbarManager.setFullscreen(true);
    FlutterStatusbarManager.setHidden(true);
  }
}

class VoidItem extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      decoration: new BoxDecoration(
          color: Color(0xFFF2F2F2),
          border: Border(
              right: BorderSide(color: Color(0xFFebedf0), width: 0.8),
              bottom: BorderSide(color: Color(0xFFebedf0), width: 0.8))),
    );
  }
}
